import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const app = getApp();
var that;
var ipUrl = app.globalData.ipUrl;

Page({
  data: {
    num: 0, //班级数
    course: [],
    selected: true, //课程选中状态
    totalPrice: 0, //总价，初始为0
    selectAllStatus: true, //全选状态，默认全选
    isDisabled: false, //如果为true启用去结算按钮
    tempArray: []
  },
  onLoad() {
    that = this;
    wx.request({
      url: ipUrl + '/buycourse/buycourse/getBuyCourse?goukekey=gouke&sid=3',
      success(res) {
        var sumYmoney = 0;
        let sumNum = res.data.length;
        for (var i = 0; i < res.data.length; i++) {
          sumYmoney += res.data[i].xiangQing.ymoney
        }
        // console.log(res.data.length)
        // console.log(sumYmoney)
        // console.log("辅导老师" + res.data[0].xiangQing.teacherdetailsf[0].tname)

        that.setData({
          course: res.data, //将结果级存入数组
          totalPrice: sumYmoney, //页面初始将金额计算出来展示
          num: sumNum,
          tempArray: res.data
        })
      }
    })
  },
  getTotalPrice() {
    let course = this.data.course; // 获取购物车列表
    let total = 0;
    let sumNum = 0;
    var temp = [];
    for (let i = 0; i < course.length; i++) { // 循环列表得到每个数据
      console.log("当前商品：" + course[i].selected)
      if (course[i].selected == true) { // 判断选中才会计算价格
        console.log("是否选中" + course[i].selected)
        total += course[i].xiangQing.ymoney; // 所有价格加起来
        sumNum += course[i].sum //数量相加
        temp.push(course[i])
      } else {
        console.log("------" + course[i].selected)
      }
    }
    this.setData({
      tempArray: temp
    })
    console.log("长度" + this.data.tempArray.length)

    if (sumNum == 0) { //如果选中数量等于零，就禁用去结算按钮
      that.setData({
        isDisabled: true
      })
    } else {
      that.setData({
        isDisabled: false
      })
    }
    this.setData({ // 最后赋值到data中渲染到页面
      course: course,
      totalPrice: total, //total.toFixed(2)带两位小数点
      num: sumNum, //将数量重新赋值

    });
    console.log("===" + that.data.totalPrice)
    console.log(that.data.tempArray.length)
  },
  selectList(e) {
    const index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    console.log(index)
    let course = this.data.course; // 获取购物车列表
    const selected = course[index].selected; // 获取当前商品的选中状态
    course[index].selected = !selected; // 改变状态
    this.setData({
      course: course
    });

    this.getTotalPrice(); // 重新获取总价
  },
  onChange(event) { //点击去结算按钮
    console.log("~~~" + event.detail)
    if (event.detail == 1) {
      //将course数组转发到订单详情页面
      wx.navigateTo({
        url: '/pages/test/test?course=' + JSON.stringify(that.data.tempArray),
      })
    }
    // this.setData({
    //   selected: event.detail,
    // });
  },
  deleteCourse(e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          //拿到当前登录学员id以及当前点击的课程id
          // console.log(app.globalData.student.stuId)
          console.log(e.currentTarget.dataset.total) //当前课程id
          Toast.loading({
            message: '删除中...',
            forbidClick: true,
          });

          wx.request({
            url: ipUrl + '/buycourse/buycourse/deleteBuyCourse?sid=3&cid=' + e.currentTarget.dataset.total,
            success(result) {
              if (result) {
                console.log("删除了")
                Toast({
                  type: 'success',
                  message: '删除成功',
                  onClose: () => {
                    that.onLoad()
                  },
                });

              } else {
                console.log("错误删除")
                Toast({
                  type: 'fail',
                  message: '服务器繁忙',
                  onClose: () => {
                    that.onLoad()
                  },
                });
              }
            }
          })

        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})